package com.xfc.quartz.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xfc.message.NoticeMessage;
import com.xfc.school.domain.*;
import com.xfc.school.service.*;
import com.xfc.wx.domain.ScWxUser;
import com.xfc.wx.service.IScWxUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * 上课提醒
 * @author lcj
 */
@Slf4j
@Component("CourseTask")
public class CourseTask {


    /**学校地址*/
    @Value("${wx.courseUrl}")
    private String courseUrl;

    @Autowired
    private NoticeMessage message;

    @Autowired
    private IScCourseScheduleService courseScheduleService;

    @Autowired
    private IScBuyCourseService buyCourseService;

    @Autowired
    private IScStudentService studentService;

    @Autowired
    private IScWxUserService wxUserService;

    @Autowired
    private IScCourseListService courseListService;

    @Autowired
    private IScCourseService courseService;

    @Autowired
    private IScTeacherService teacherService;

    public void courseMessage(){
        log.info("上课提醒开始:{}", DateUtil.now());
        Date newDate = DateUtil.offset( DateUtil.date(), DateField.DAY_OF_MONTH, 1);
        //查询要上课的课程
        QueryWrapper<ScCourseSchedule> queryWrapper=new QueryWrapper<>();
        queryWrapper.apply(Objects.nonNull(newDate),
                "date_format (start_time,'%Y-%m-%d') = date_format('" +  DateUtil.format(newDate, "yyyy-MM-dd") + "','%Y-%m-%d')");
        queryWrapper.eq("course_state",0);
        List<ScCourseSchedule> courseList=courseScheduleService.list(queryWrapper);
        courseList.forEach(l->{
            //查询课程名称
            ScCourse scCourse=courseService.getById(l.getCourseNumber());

            //查询老师名称
            ScCourseList scCourseList=courseListService.getById(l.getCourseListId());
            if (Objects.nonNull(scCourseList)){
                //查询老师信息
                ScTeacher teacher=teacherService.getById(scCourseList.getTeacherId());
                if (Objects.nonNull(teacher)){
                    scCourseList.setTeacherName(teacher.getTeacherName());
                }else {
                    log.error("老师信息查询为空,teacher-id:{}",scCourseList.getTeacherId());
                }
            }else {
                log.error("老师信息查询为空,scCourseList-id:{}",l.getCourseListId());
            }
            //查询学生购买课程
            QueryWrapper<ScBuyCourse> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("class_id",l.getCourseNumber());
            queryWrapper1.gt("class_surplus_time",0);
            List<ScBuyCourse> buyCourseList=buyCourseService.list(queryWrapper1);
            buyCourseList.forEach(b->{
                //查询学生信息
                ScStudent student=studentService.getById(b.getStudentId());
                if (Objects.nonNull(student)){
                    //查询openid
                    ScWxUser wxUser=wxUserService.getOne(new QueryWrapper<ScWxUser>().eq("mobile",student.getStudentMobile()));
                    if (Objects.nonNull(wxUser)){
                        //发送通知
                        message.courseMessage(wxUser.getOpenId(),Objects.isNull(scCourse.getClassName())?"":scCourse.getClassName(),courseUrl,scCourseList.getTeacherName(),DateUtil.format(l.getStartTime(),"yyyy-MM-dd HH:mm:ss") );
                    }else {
                        log.error("没有查询到用户openid,用户手机：{}",student.getStudentMobile());
                    }
                }else {
                    log.error("学生信息不能为空,购买订单id:{}",b.getId());
                }
            });
        });
        log.info("上课提醒结束:{}", DateUtil.now());
    }
}
